package com.qing;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class Demo3 {

	public static void main(String[] args) {
		long uniqueWords=0;
		try {
			//流会自动关闭，因此不需要额外的try-finally操作
			Stream<String> lines= Files.lines(Paths.get("data.txt"), Charset.defaultCharset());

			uniqueWords=lines.flatMap(line->Arrays.stream(line.split(" ")))  //生成单词
					.distinct() //去重
					.count(); //统计数量

			System.out.println(uniqueWords);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
